﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace _201206QuanLyBanHang.BaoCao
{
    public partial class FormHienThiKhachHang : Form
    {        
        //define;
        DataSet ds_dskh = new DataSet();
        private SQLCommand msql = new SQLCommand();
        private DataTable dt = new DataTable();  
        private int _makv;
        private string _tenkv,_nguoinhan,_nguoixuat;
        public ReportDocument report;

        public FormHienThiKhachHang()
        {
            InitializeComponent();
        }
        //lay 1 gia tri tu form khac
        public void getData(int makvkh)
        {
            _makv = makvkh;
        }
        //lay 1 text tu form khac
        public void getDatatxt(string str)
        {
            _tenkv = str;
        }
        public void getDatanguoinhan(string str)
        {
            _nguoinhan = str;
        }
        public void getDatanguoixuat(string str)
        {
            _nguoixuat = str;
        }
        // lay datatable tu form khac
        public void getvaluefromdgv(DataTable _dttable)
        {
            dt = _dttable;            
        }
        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            ParameterDiscreteValue tenkv= new ParameterDiscreteValue();
            ParameterDiscreteValue nguoinhan = new ParameterDiscreteValue();
            ParameterDiscreteValue nguoixuat = new ParameterDiscreteValue();
            ParameterDiscreteValue tongsl = new ParameterDiscreteValue();
            ParameterValues tenkv_vl = new ParameterValues();
            ParameterValues nguoinhan_vl = new ParameterValues();
            ParameterValues nguoixuat_vl = new ParameterValues();
            ParameterValues tongsl_vl = new ParameterValues();

            DataTable dtsoluong = new DSDSKH.soluongDataTable();          
            dtsoluong.AcceptChanges();         
            dtsoluong = dt.Copy();
            int total = 0;
            for (int i = 0; i < dtsoluong.Rows.Count; i++)
            {
               // total = total + int.Parse(dtsoluong.Rows[i]["sl"].ToString());   
            }
            //main report
            string sqlstr_makv = "select kh.Tencuahang,sp.masp as loai,(sp.giasp + ck.Giachietkhau) as Giaban , ck.ngaychietkhau from khachhang kh inner  join chietkhau ck on kh.makh = ck.makh inner join sanpham sp on sp.masp =ck.masp where kh.makhuvuc=" + _makv + " and tinhtrangkh=1";
            ds_dskh = msql.getDMKhachHang(sqlstr_makv, "khachhang");          
            report = new ReportDocument();
            report.Load("..\\..\\BaoCao\\CrystalReport_KH.rpt");
            report.SetDataSource(ds_dskh.Tables["khachhang"]);
            //subreport
            report.Subreports["soluongsp"].SetDataSource(dtsoluong);
            report.Subreports["soluongtoncuoi"].SetDataSource(dtsoluong);
            //end subreport
            //hien thi ten khu vuc           
            tenkv.Value = _tenkv;
            nguoinhan.Value = _nguoinhan;
            nguoixuat.Value = _nguoixuat;
            tongsl.Value = total;
            tenkv_vl.Add(tenkv);
            nguoinhan_vl.Add(nguoinhan);
            nguoixuat_vl.Add(nguoixuat);
            tongsl_vl.Add(tongsl);

            report.DataDefinition.ParameterFields["khuvucgh"].ApplyCurrentValues(tenkv_vl);
            report.DataDefinition.ParameterFields["Nnhanhang"].ApplyCurrentValues(nguoinhan_vl);
            report.DataDefinition.ParameterFields["Nxuathang"].ApplyCurrentValues(nguoixuat_vl);
            report.DataDefinition.ParameterFields["tongsl"].ApplyCurrentValues(tongsl_vl);
            //
            crystalReportViewer1.ReportSource = report;
            
        }

     }
}
